package com.uniknow.app.ui;

import java.util.List;

import roboguice.inject.InjectView;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uniknow.app.R;
import com.uniknow.app.bin.WixAccountBean;
import com.uniknow.app.manager.UserManager;
import com.uniknow.app.operate.BaseListener;
import com.uniknow.app.operate.CommonOperate;
import com.uniknow.app.operate.impl.CommonOperateImpl;
import com.uniknow.app.ui.dialog.DialogAddWixAccount;
import com.uniknow.app.util.ImageUtils;
import com.uniknow.app.util.ViewUtil;
import com.uniknow.app.widget.ScrollLayout;

public class MyAccountActivity extends BaseActivity implements OnClickListener{
	private CommonOperate operate;
	private int formxScroll,fromxTable,curIndex;// 游标所在X轴
	private LayoutInflater listContainer;//视图容器
	private int itemViewResource;//自定义项视图源
	private DialogAddWixAccount dialogAddWix;
	
	@InjectView(R.id.activity_myaccount_layout)
	private ScrollLayout mScrollLayout;
	@InjectView(R.id.activity_myaccount_imgcur)
	private ImageView mScrollCursor;
	@InjectView(R.id.activity_myaccount_tabwix)
	private TextView tabwix;
	@InjectView(R.id.activity_myaccount_tabsina)
	private TextView tabsina;
	@InjectView(R.id.myaccount_wix)
	private LinearLayout myAccountWix;
	@InjectView(R.id.myaccount_buywix)
	private LinearLayout myAccountBuyWix;
	@InjectView(R.id.myaccount_wix_panel)
	private LinearLayout myAccountWixPanel;
	@InjectView(R.id.myaccount_buywix_panel)
	private LinearLayout myAccountBuyWixPanel;
	@InjectView(R.id.myaccount_wix_add)
	private Button wixAddBtn;
	
	static class ListItemView{//自定义控件集合  
		public TextView account;
        public TextView income;
        public CheckBox isSend;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		curIndex = intent.getIntExtra("curIndex", 0);
		setContentView(R.layout.activity_myaccount);
		mContext = this;
		operate = CommonOperateImpl.getOperate(mContext);
		this.listContainer = LayoutInflater.from(mContext);	//创建视图容器并设置上下文
		this.itemViewResource = R.layout.item_wixaccount_show;
		initView();
	}
	
	public void initView(){
		setTitle("我的渠道");
		tabwix.setOnClickListener(this);
		tabsina.setOnClickListener(this);
		myAccountWix.setOnClickListener(this);
		myAccountBuyWix.setOnClickListener(this);
		wixAddBtn.setOnClickListener(this);
		//计算游标
		initScrollLayout();
	}
	
	public void initScrollLayout(){
    	final int bmpW = ViewUtil.getScreenWidth() / mScrollLayout.getChildCount();//计算图片显示宽度
    	formxScroll = mScrollLayout.getCurScreen();// 游标所在X轴
    	Bitmap mcursor = ImageUtils.drawableToBitmap(mScrollCursor.getDrawable());
    	mScrollCursor.setImageBitmap(ImageUtils.zoomBitmap(mcursor, bmpW, 5));
    	mScrollLayout.SetOnViewChangeListener(new ScrollLayout.OnViewChangeListener() {
			@Override
			public void OnViewChange(int viewIndex) {
				curIndex = viewIndex;
				Animation animation = null;
				animation = new TranslateAnimation(formxScroll*bmpW, viewIndex*bmpW, 0, 0);
				animation.setFillAfter(true);// True:图片停在动画结束位置
				animation.setDuration(200);
				mScrollCursor.startAnimation(animation);
				formxScroll = mScrollLayout.getCurScreen();
			 }
			
		 });
    	mScrollLayout.setToScreen(curIndex);
    	initMyAccountData();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.activity_myaccount_tabwix:
			if(0 == mScrollLayout.getCurScreen())return;
			mScrollLayout.scrollToScreen(0);
			break;
		case R.id.activity_myaccount_tabsina:
			if(1 == mScrollLayout.getCurScreen())return;
			mScrollLayout.scrollToScreen(1);
			break;
		case R.id.myaccount_wix:
			if(myAccountWixPanel.getVisibility() == View.GONE){
				myAccountWixPanel.setVisibility(View.VISIBLE);
			}else{
				myAccountWixPanel.setVisibility(View.GONE);
			}
			break;
		case R.id.myaccount_buywix:
			if(myAccountBuyWixPanel.getVisibility() == View.GONE){
				myAccountBuyWixPanel.setVisibility(View.VISIBLE);
			}else{
				myAccountBuyWixPanel.setVisibility(View.GONE);
			}
			break;
		case R.id.myaccount_wix_add:
			if(dialogAddWix == null){
				dialogAddWix = new DialogAddWixAccount(mContext);
				dialogAddWix.show();
			}else{
				dialogAddWix.show();
			}
			break;
		}
		
	}
	
	private void initMyAccountData(){
		showLoading();
		operate.getWixAccount(UserManager.userBean, new BaseListener() {
			
			@Override
			public void onSuccess(Object o) {
				closeLoading();
				List<WixAccountBean> retList = (List<WixAccountBean>) o;
				for(WixAccountBean wixBean:retList){
					final View convertView = listContainer.inflate(itemViewResource, null);
					ListItemView  listItemView = new ListItemView();
					listItemView.account = (TextView) convertView.findViewById(R.id.item_wix_account);
					listItemView.income = (TextView) convertView.findViewById(R.id.item_wix_income);
					listItemView.isSend = (CheckBox) convertView.findViewById(R.id.item_wix_issend);
					listItemView.account.setText(wixBean.getDsc());
					//TODO 
					myAccountWixPanel.addView(convertView);
				}
			}
			
			@Override
			public void onError(String e) {
				closeLoading();
				
			}
		});
	}
	

}
